package org.lq.his.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.lq.his.pojo.Drug;
import org.lq.his.pojo.Myo;
import org.lq.his.service.DrugService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/drug")
public class DrugController {
	@Resource
	private	DrugService drugService;

	public void setDrugService(DrugService drugService) {
		this.drugService = drugService;
	}
	@RequestMapping("/drugIndexs")
	public String drugIndex()throws Exception{
		return "drug/drugIndex";
	}
	@RequestMapping(value="/drugForm",method=RequestMethod.GET)
	public String drugAdd()throws Exception{
		return "drug/drugForm";
	}
	
	@RequestMapping("/list") 
	@ResponseBody
	public Map<String, Object> list(@RequestParam(defaultValue="1")Integer  page,@RequestParam(defaultValue="5")Integer  rows,@RequestParam(defaultValue="id") String sort,@RequestParam(defaultValue="asc") String order, Drug condition) throws Exception{
		
		Map<String, Object> map = new HashMap<>();
		int start = (page - 1)*rows;
		List<Drug> list = drugService.getListByCondition(start, rows, condition, sort,order);
		
		int total = drugService.getCountByCondition(condition);
		
		map.put("total", total);
		map.put("rows", list);
		
		return map;
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(Drug drug)throws Exception{
		Map<String, Object> map = new HashMap<>();
		drugService.add(drug);
		map.put("result", true);
		return map;
	}
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> edit(Drug drug)throws Exception{
		Map<String, Object> map = new HashMap<>();
		drugService.update(drug);
		
		map.put("result", true);
		
		return map;
	}
	@RequestMapping("/view")
	@ResponseBody
	public Drug view(Integer id)throws Exception{
		return drugService.getById(id);
	}
	@RequestMapping("/deleteDrug")
	@ResponseBody
	public Map<String, Object> deleteDrug(Integer[] ids)throws Exception{
		drugService.deleteDrug(ids);
		Map<String, Object> map= new HashMap<>();
		map.put("result", true);
		return map;
	}
	@RequestMapping("/PrescribeId")
	@ResponseBody
	public List<Drug> getDrugsByPrescribeId(Integer prescribeId)throws Exception{
		return  drugService.getDrugsByPrescribeId(prescribeId);
	}

}
